package com.shinewind.info.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.BooleanUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.info.service.ICommentActionService;
import com.shinewind.info.service.ICommentService;
import com.shinewind.info.service.IUserDetailService;
import com.shinewind.info.service.IUserInfoService;
import com.shinewind.pojo.dto.PageDto;
import com.shinewind.pojo.dto.ParentCommentDto;
import com.shinewind.pojo.dto.ParentDeleteCommentDto;
import com.shinewind.pojo.entity.Comment;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.CommentAllVo;
import com.shinewind.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/modelEvaluating")
@Api(value = "评论接口",description = "用于展示模型的评测文章中的评论信息")
public class CommentController {
    @Autowired
    private ICommentService iCommentService;
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IUserDetailService iUserDetailService;
    @Autowired
    private ICommentActionService iCommentActionService;

    @PostMapping("/article/comment/list/{aId}")
    @ApiOperation(value = "评论信息",notes = "评论列表信息",response = Result.class)
    public PageVo<CommentAllVo> getCommentListByPage(@PathVariable("aId") Integer aId, @RequestBody(required = false) PageDto pageDto){
        //根据文章id查询所有评论
        List<Comment> comments = iCommentService.getCommentListById(aId);
        //嵌套评论
        List<CommentAllVo> commentAllVoList = this.getAllComments("0",comments);
        int total = commentAllVoList.size();
        commentAllVoList = ListUtil.page(pageDto.getCurrentPage()-1,pageDto.getPageSize(),commentAllVoList);
        return new PageVo<CommentAllVo>(commentAllVoList, BooleanUtil.isTrue(commentAllVoList.size()>0),
                messageConstant.QUERY_SUCCESS,total);
    }
    private List<CommentAllVo> getAllComments(String cid,List<Comment> comments) {
        //根据文章关联的用户id查询用户昵称
        List<CommentAllVo> commentAllVoList = new ArrayList<>();
        for(Comment comment : comments){
            if(cid.equals(comment.getParentId())){
                CommentAllVo commentAllVo = CommentAllVo.builder().build();
                BeanUtils.copyProperties(comment,commentAllVo);
                commentAllVo.setNickName(iUserInfoService.getUserById(comment.getUId()).getNickName());
                commentAllVo.setAvatar(iUserDetailService.getUserDetailById(comment.getUId()).getAvatar());
                commentAllVo.setLove(iCommentActionService.getSumLoveBycId(comment.getCId()));
                commentAllVo.setChildren(this.getAllComments(comment.getCId(),comments));
                commentAllVoList.add(commentAllVo);
            }
        }
        return commentAllVoList;
    }

    @PostMapping("/article/parent/comment")
    @ApiOperation(value = "添加评论",notes = "添加评论",response = Result.class)
    public Result addParentComment(@RequestBody ParentCommentDto parentCommentDto){
        System.out.println("parentCommentDto:"+parentCommentDto);
        Boolean flag = iCommentService.saveParentCommentByDto(parentCommentDto);
        if(flag)
            return Result.success(true,
                    messageConstant.INSERT_SUCCESS);
        else
            return Result.fail(false,messageConstant.INSERT_ERROR);
    }

    @GetMapping("/actions/list/{uId}")
    @ApiOperation(value = "返回当前用户已经点赞的评论列表",notes = "点赞",response = Result.class)
    public Result getLoveList(@PathVariable("uId") String uId){
        List<String> loveList = iCommentActionService.getLoveListByUid(uId);
        return Result.success(loveList,BooleanUtil.isTrue(!loveList.stream().findFirst().equals("你还没有任何点赞")),
                messageConstant.QUERY_SUCCESS);
    }

    @PutMapping("/comment/actions/cancel/love/{cId}/{uId}")
    @ApiOperation(value = "取消点赞",notes = "取消点赞",response = Result.class)
    public Result cancelLove(@PathVariable("cId") String cId,@PathVariable("uId") String uId){
        Boolean flag = iCommentActionService.cancelLoveByUidANDcId(cId,uId);
        if(flag)
            return Result.success(true,
                    messageConstant.UPDATE_SUCCESS);
        else
            return Result.fail(false,messageConstant.UPDATE_ERROR);
    }

    @PostMapping("/comment/actions/add/love/{cId}/{uId}")
    @ApiOperation(value = "添加点赞",notes = "添加点赞",response = Result.class)
    public Result addLove(@PathVariable("cId") String cId,@PathVariable("uId") String uId){
        Boolean flag = iCommentActionService.addLoveByUidANDcId(cId,uId);
        if(flag)
            return Result.success(true,
                    messageConstant.INSERT_SUCCESS);
        else
            return Result.fail(false,messageConstant.INSERT_SUCCESS);
    }

    @PutMapping("/comment/delete")
    @ApiOperation(value = "删除评论",notes = "删除评论",response = Result.class)
    public Result deleteCommentByCid(@RequestBody ParentDeleteCommentDto parentDeleteCommentDto){
        System.out.println("parentDeleteCommentDto:"+parentDeleteCommentDto);
        Boolean flag = iCommentService.deleteCommentBycId(parentDeleteCommentDto);
        if(flag)
            return Result.success(true,
                    messageConstant.DELETE_SUCCESS);
        else
            return Result.fail(false,messageConstant.DELETE_ERROR);
    }

    @PutMapping("/comment/child/delete")
    @ApiOperation(value = "删除二级评论",notes = "删除二级评论",response = Result.class)
    public Result deleteChildCommentByCid(@RequestBody ParentDeleteCommentDto parentDeleteCommentDto){
        System.out.println("parentDeleteCommentDto:"+parentDeleteCommentDto);
        Boolean flag = iCommentService.deleteChildCommentBycId(parentDeleteCommentDto);
        if(flag)
            return Result.success(true,
                    messageConstant.DELETE_SUCCESS);
        else
            return Result.fail(false,messageConstant.DELETE_ERROR);
    }
}
